package jia.tools;

import java.util.Arrays;
import java.math.BigDecimal;
import jia.game.GenericFlags;
import org.apache.log4j.Logger;

/**
 * Class that generates attribute statistics for popular gaming systems.
 * @author Erik Stell
 * @version 1.0
 */
public class StatisticCalculator {
    private static final Logger log = Logger.getLogger(StatisticCalculator.class);
    
    /**
     * Special method to faciliate a character's statistic roll.  The method
     * uses the rollD10 +10 method, of rolling 1 ten sided dice, and adding it to a
     * a base of ten.<br>
     * <br>
     * The characters race and gender are passed in to allow a modifier to be calculated that might
     * enhance or hinder the final score.
     * @return int value of the returned "roll"
     */
    public static int rollStatisticBase10(String race, String gender) {
        int retVal = 1;
        int modifier = 0;
        
        //determine the modifier based upon race and gender
        
        //calculate the modifier for the race and gender of the character in question.
        try {
            //roll 4d6, setting each to the value of a unique variable
            int roll1 = StaticDiceRoller.rolldSomething(10, 1);
            int[] tempArray = { roll1, 10, modifier };
            
            //add the three remaining values in the arrayList to get the value
            retVal = tempArray[0] + tempArray[1] + modifier;
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        
        return retVal;
    }
    
    
    /**
     * Special method to faciliate a character's statistic roll.  The method
     * uses the 4d6 method, of rolling 4 six sided dice, dropping the lowest
     * value, and adding the rest to obtain the attribute score.<br>
     * <br>
     * The characters race and gender are passed in to allow a modifier to be calculated that might
     * enhance or hinder the final score.
     * @return int value of the returned "roll"
     */
    public static int rollStatistic4D6(String race, String gender) {
        int retVal = 1;
        int modifier = 0;
        
        //determine the modifier based upon race and gender
        
        //calculate the modifier for the race and gender of the character in question.
        try {
            //roll 4d6, setting each to the value of a unique variable
            int roll1 = StaticDiceRoller.rolldSomething(6, 1);
            int roll2 = StaticDiceRoller.rolldSomething(6, 1);
            int roll3 = StaticDiceRoller.rolldSomething(6, 1);
            int roll4 = StaticDiceRoller.rolldSomething(6, 1);
            //throw the values into an arraylist, and sort the arrayList in ascending order
            int[] tempArray = {roll1, roll2, roll3, roll4};
            
            Arrays.sort(tempArray);
            //add the three remaining values in the arrayList to get the value
            retVal = tempArray[0] + tempArray[1] + tempArray[2] + modifier;
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        
        return retVal;
    }
    
    /**
     * Calculates the average of a characters stats, to return what is called
     * a Prime Modifier.  This number is what is used as the basis for all other
     * numerical calculattions
     * @param str - The Strength attribute value
     * @param dex - The Dexterity attribute value
     * @param intel - The Intelligence attribute value
     * @param wis - The Wisdon attribute value
     * @param con - The Constitution attribute value
     * @param cha - The Charisma attribute value
     * @return int value comprising an average of all the stats
     */
    public static int calcPrimeModifier(float str,
        float dex,
        float intel,
        float wis,
        float con,
        float cha) {
        int retVal = 0;
        
        try {
            //add the values
            float totalVal = (str + dex + intel + wis + con + cha);
            float avgVal = totalVal / 6;
            
            //double small = totalVal/6;
            retVal = (new BigDecimal(avgVal)).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        
        return retVal;
    }
    
    /**
     * Generates a statistic for the Dnd 3rd edition ruleset, also known as d20
     * @param race - the race name of the character
     * @param charClass - the chosen character class for this character
     * @param gender - the gender (Male or Female) of the character
     * @return int value of the statistic
     */
    public int tsrGenerateStatD20(String race, String charClass, String gender) {
        int retVal = 0;
        try {
            retVal = rollStatistic4D6(race, gender);
            
            //add any class specific bonus's to the roll
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        return retVal;
    }
    
    /**
     * Generates a statistic for the Dnd 2ed rules.
     * @param race - The race name of the character
     * @param gender - the gender (Male or Female) of the character
     * @return int value of the statistic
     */
    public int tsrGenerateStat2ed(String race, String gender) {
        int retVal = 0;
        try {
            retVal = rollStatistic4D6(race, gender);
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        return retVal;
    }
    
    /**
     * Generates a statistic for the Logical Role Playing Game System, also
     * known as LRP.
     * @param race - The race name of the character
     * @param gender - the gender (Male or Female) of the character
     * @return int value of the statistic
     */
    public int lrpGenerateStat(String race, String gender) {
        int retVal = 0;
        try {
            retVal = rollStatisticBase10(race, gender);
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        return retVal;
    }
    
    
    /**
     * Generates a statistic for the Shadowrun Game System
     * @return int value of the statistic
     * @deprecated - This feature is not yet implemented, will return 0
     */
    public int shadowRunGenerateStat() {
        int retVal = 0;
        
        return retVal;
    }
    
    /**
     * Generates a statistic for the d20 Star Wars Game System
     * @return int value of the statistic
     * @deprecated - This feature is not yet implemented, will return 0
     */
    public int starWarsGenerateStat() {
        int retVal = 0;
        
        return retVal;
    }
}